//
//  RemindModel.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/15.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "RemindModel.h"
#import "NSDate+FormatterString.h"

@implementation RemindModel
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"ids"  : @"id"};
}

- (void)setRemind_time:(NSString *)remind_time
{
    _date = [NSDate getDateWithStringSince1970ToSecond:remind_time stringDateFormatter:@"YYYY年MM月dd日"];
    _time = [NSDate getDateWithStringSince1970ToSecond:remind_time stringDateFormatter:@"HH:mm"];
}

- (NSString *)getDateString
{
    NSString *string = [_date stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
    string = [string stringByReplacingOccurrencesOfString:@"月" withString:@"-"];
    string = [string stringByReplacingOccurrencesOfString:@"日" withString:@" "];
    return [NSString stringWithFormat:@"%@%@",string,_time];
}

- (RemindModel *)handelRemindResult:(NSString *)result
{
    [self handleResult:result];
    return self;
}

- (void)handleResult:(NSString *)result
{
    if ([result containsString:@"提醒"])
    {
        NSArray *dateArray = @[@"今天上午",@"今天下午",@"明天上午",@"明天下午",@"后天上午",@"后天下午",@"大后天上午",@"大后天下午"];
        BOOL hasDateArray = YES;
        for (NSString *dateString in dateArray)
        {
            if ([result containsString:dateString])
            {
                
                hasDateArray = YES;
                
                [self handleTimeWithResult:result date:dateString];
                break;
            }
            else
            {
                hasDateArray = NO;
            }
        }
        
        if (!hasDateArray)
        {
            NSError *error;
            // 创建NSRegularExpression对象并指定正则表达式
            NSRegularExpression *regex = [NSRegularExpression
                                          regularExpressionWithPattern:@"[0-9]{4}年(0?[0-9]|1[0-2])月(0?[1-9]|[12]?[0-9]|3[01])日"
                                          options:0 error:&error];
            if (!error) { // 如果没有错误
                // 获取特特定字符串的范围
                NSTextCheckingResult *match = [regex firstMatchInString:result
                                                                options:0
                                                                  range:NSMakeRange(0, [result length])];
                if (match) {
                    // 截获特定的字符串
                    NSString *dateString = [result substringWithRange:match.range];
                    
                    [self handleTimeWithResult:result date:dateString];
                }
            } else { // 如果有错误，则把错误打印出来
                NSLog(@"error - %@", error);
            }
        }
    }
    else
    {
        return;
    }
}

- (void)handleTimeWithResult:(NSString *)result date:(NSString *)date
{
    NSString *title = [NSString string];
    NSRange range = [result rangeOfString:date];
    title = [result substringToIndex:range.location];
    //    NSLog(@"%@----%@",title,date);
    
    NSString *time = [self handelTimeWithResult:[result substringFromIndex:range.location]];
    _content = title;
    _date = date;
    _time = time;
//    NSLog(@"%@----%@-----%@",title,date,time);
}

- (NSString *)handelTimeWithResult:(NSString *)result
{
    NSString *time = [NSString string];
    if([result containsString:@"日"])
    {
        NSRange range = [result rangeOfString:@"日"];
        if (result.length > range.location + 1)
        {
            time = [result substringFromIndex:range.location + 1];
        }
    }
    else if ([result containsString:@"天"])
    {
        NSRange range = [result rangeOfString:@"天"];
        if (result.length > range.location + 1)
        {
            time = [result substringFromIndex:range.location + 1];
        }
    }
    
    NSError *error;
    // 创建NSRegularExpression对象并指定正则表达式
    NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:@"([0-24])"
                                  options:0 error:&error];
    if (!error) { // 如果没有错误
        // 获取特特定字符串的范围
        NSTextCheckingResult *match = [regex firstMatchInString:time
                                                        options:0
                                                          range:NSMakeRange(0, [time length])];
        if (match) {
            // 截获特定的字符串
            NSString *dateString = [time substringWithRange:match.range];
            if ([result containsString:@"点半"])
            {
                if ([time containsString:@"上午"])
                {
                    return [NSString stringWithFormat:@"%@:30",dateString];
                }
                else if ([time containsString:@"下午"])
                {
                    return [NSString stringWithFormat:@"%i:30",[dateString intValue]+12];
                }
            }
            else if ([result containsString:@"点"])
            {
                if ([time containsString:@"上午"])
                {
                    return [NSString stringWithFormat:@"%@:00",dateString];
                }
                else if ([time containsString:@"下午"])
                {
                    return [NSString stringWithFormat:@"%i:00",[dateString intValue]+12];
                }
            }
            else
            {
                NSArray *array = [time componentsSeparatedByString:@":"];
                NSString *timeString = [NSString stringWithFormat:@"%02d:%02d",[array.firstObject intValue],[array.lastObject intValue]];
                return timeString;
                //                // 创建NSRegularExpression对象并指定正则表达式
                //                NSRegularExpression *regex = [NSRegularExpression
                //                                              regularExpressionWithPattern:@"(([0-1]{1}[0-9]{1})|([2]{1}[0-4]{1}))([:])(([0-5]{1}[0-9]{1}|[6]{1}[0]{1}))"
                //                                              options:0 error:&error];
                //                if (!error) { // 如果没有错误
                //                    // 获取特特定字符串的范围
                //                    NSTextCheckingResult *match = [regex firstMatchInString:timeString
                //                                                                    options:0
                //                                                                      range:NSMakeRange(0, [timeString length])];
                //                    if (match) {
                //                        // 截获特定的字符串
                //                        NSString *dateString = [result substringWithRange:match.range];
                //                        return dateString;
                //                        //                        [self handleTimeWithResult:result date:dateString];
                //                    }
                //                } else { // 如果有错误，则把错误打印出来
                //                    NSLog(@"error - %@", error);
                //                    return nil;
                //                }
            }
            
        }
        else
        {
            NSArray *array = [time componentsSeparatedByString:@":"];
            NSString *timeString = [NSString stringWithFormat:@"%02d:%02d",[array.firstObject intValue],[array.lastObject intValue]];
            return timeString;
        }
    } else { // 如果有错误，则把错误打印出来
        NSLog(@"error - %@", error);
    }
    return nil;
}

@end
